package com.levy.proxy;

import java.lang.reflect.Proxy;

public class ProxyMain {

	public static void main(String[] args) {
//		testLogInvocationHandler();
		testCGLibAOP();

	}

	public static void testLogInvocationHandler() {
		// 需要代理的接口，被代理类实现的多个接口都必须在这里定义
		Class[] proxyInterface = new Class[] { IBusiness.class, IBusiness2.class };

		// 构建AOP的Advice，这里需要歘如业务的实例
		LogInvocationHandler handler = new LogInvocationHandler(new Business());

		// 生成代理类的字节码加载器
		ClassLoader classLoader = ProxyMain.class.getClassLoader();

		// 织入器，织入代码并生成代理类
		IBusiness2 proxyBusiness = (IBusiness2) Proxy.newProxyInstance(classLoader, proxyInterface, handler);

		// 使用代理类的实例调用方法
		proxyBusiness.doSomeThing2();
		((IBusiness) proxyBusiness).doSomeThing();
	}

	public static void testCGLibAOP() {
		CGLibProxy factory = new CGLibProxy();
		PersonServiceBean service = (PersonServiceBean) factory.createProxyInstance(new PersonServiceBean("xxlong"));
		service.save("999");
		
		PersonServiceBean service1 = (PersonServiceBean) factory.createProxyInstance(new PersonServiceBean("levy.xia"));
		service1.save("888");
		service.save("999");
		service.save("999");
	}
}
